package com.netease.musicweekly.xml;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;

import com.netease.musicweekly.entity.DocumentMenuEntity;

public class DocListParser extends BaseXMLParser {

	public List<DocumentMenuEntity> getDocList(String xml) throws IOException,
			XMLParseException, SAXException {
		final DocumentMenuEntity currentDoc = new DocumentMenuEntity();
		RootElement root = new RootElement("channel");
		final ArrayList<DocumentMenuEntity> docs = new ArrayList<DocumentMenuEntity>();
		Element item = root.getChild("item");
		item.setEndElementListener(new EndElementListener() {
			public void end() {
				docs.add(currentDoc.copy());
			}
		});
		item.getChild("title").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						currentDoc.setTitle(body);
					}
				});
		item.getChild("category").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String category) {
						currentDoc.setCategory(category);
					}
				});
		item.getChild("image").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						currentDoc.setDocImageUrl(body);
					}
				});
		item.getChild("posttime").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String posttime) {
						currentDoc.setPosttime(posttime);
					}
				});
		item.getChild("localtion").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String localtion) {
						currentDoc.setLocation(localtion);
					}
				});
		Xml.parse(xml, root.getContentHandler());
		return docs;
	}

}
